﻿using BLL;
using DAL;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Data.Linq;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class admincp_Order_Payment : System.Web.UI.Page
{
    DigitalWorldDataContext db = new DigitalWorldDataContext();
    OrdersBLL od = new OrdersBLL();
    order o = new order();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Session["OrderID"] = null;
            string vitri = Page.RouteData.Values["ViTri"].ToString();

            hlOrders.NavigateUrl = "~/Quan-Ly-Dat-Hang-Thanh-Toan/" + vitri + "/" + 0;
            hlPayments.NavigateUrl = "~/Quan-Ly-Dat-Hang-Thanh-Toan/" + vitri + "/" + 1;
            hlOrderDetail.NavigateUrl = "~/Quan-Ly-Dat-Hang-Thanh-Toan/" + vitri + "/" + 2;
            string id = Page.RouteData.Values["id"].ToString();
            if (id == "1")
            {
                MutiView1.ActiveViewIndex = 1;
                //this.loadData_Customer();
                rbtnStatus_Payment.Enabled = false;
                btnUpdate2.Enabled = false;
            }
            else if (id == "2")
            {
                MutiView1.ActiveViewIndex = 2;
            }
            else
            {
                MutiView1.ActiveViewIndex = 0;
                //loadPosition();
                this.LoadData_Orders();
                rb_Status.Enabled = false;
                btnUpdate.Enabled = false;
            }
        }
    }

    #region Orders
    void LoadData_Orders()
    {
        var danhsach = from ds in db.employees
                       select new
                       {
                           employeeid = ds.employeeid,
                           fullname = ds.lastname + ' ' + ds.firstname
                       };
      
        ddlEmployees.DataSource = danhsach;
        ddlEmployees.DataTextField = "fullname";
        ddlEmployees.DataValueField = "employeeid";
        ddlEmployees.DataBind();

        ddlFreight.DataSource = db.City_Freights;
        ddlFreight.DataTextField = "CityName";
        ddlFreight.DataValueField = "Freight";
        ddlFreight.DataBind();
        txtFreight.Text = "20000";

        ddlCustomers.DataSource = db.customers;
        ddlCustomers.DataTextField = "customername";
        ddlCustomers.DataValueField = "customerid";
        ddlCustomers.DataBind();

        gvOrders.DataSource = db.orders;
        gvOrders.DataBind();
    }

    void Reset_Orders()
    {
        ddlEmployees.SelectedIndex = 0;
        ddlCustomers.SelectedIndex = 0;
        ddlFreight.SelectedIndex = 0;
        txtOrderDate.Text = string.Empty;
        txtRequiredDate.Text = string.Empty;
        txtShipName.Text = string.Empty;
        txtShipAddress.Text = string.Empty;
        txtShipCity.Text = string.Empty;
        txtShipPostalCode.Text = string.Empty;
        txtShipCountry.Text = string.Empty;
        rb_Status.SelectedIndex = 1;
        txtShipName.Focus();
    }

    protected void btnInsert_Click(object sender, EventArgs e)
    {
        o.employeeid = int.Parse(ddlEmployees.SelectedValue);
        o.customerid = int.Parse(ddlCustomers.SelectedValue);
        o.orderdate = DateTime.Now.Date;
        o.requireddate = DateTime.Parse(txtRequiredDate.Text);
        o.vat = 10;
        o.freight = Decimal.Parse(txtFreight.Text);
        o.shipname = txtShipName.Text;
        o.shipaddress = txtShipAddress.Text;
        o.shipcity = txtShipCity.Text;
        o.shippostalcode = txtShipPostalCode.Text;
        o.shipcountry = txtShipCountry.Text;

        od.Order_Insert(o);
        this.LoadData_Orders();
        Reset_Orders();
    }

    private void ViewOrders()
    {
        int OrderID = int.Parse(Session["OrderID"].ToString());
        var order = db.orders.Where(o => o.orderid == OrderID).First();

        ddlEmployees.SelectedValue = order.employeeid.ToString();
        ddlCustomers.SelectedValue = order.customerid.ToString();
        txtOrderDate.Text = DateTime.Parse(order.orderdate.ToString()).ToString("MM/dd/yyyy");
        txtRequiredDate.Text = DateTime.Parse(order.requireddate.ToString()).ToString("MM/dd/yyyy");
        txtFreight.Text = order.freight.ToString().Substring(0,order.freight.ToString().LastIndexOf('.'));
        txtShipName.Text = order.shipname.ToString();
        txtShipAddress.Text = order.shipaddress.ToString();
        txtShipCity.Text = order.shipcity.ToString();
        txtShipPostalCode.Text = order.shippostalcode.ToString();
        txtShipCountry.Text = order.shipcountry.ToString();
        rb_Status.SelectedValue = order.hide.ToString();
        rb_Status.Enabled = true;
        btnUpdate.Enabled = true;

    }

    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        o.orderid = int.Parse(Session["OrderID"].ToString());
        o.employeeid = int.Parse(ddlEmployees.SelectedValue);
        o.customerid = int.Parse(ddlCustomers.SelectedValue);
        o.orderdate = DateTime.Parse(txtOrderDate.Text);
        o.requireddate = DateTime.Parse(txtRequiredDate.Text);
        o.freight = decimal.Parse(txtFreight.Text);
        o.shipname = txtShipName.Text;
        o.shipaddress = txtShipAddress.Text;
        o.shipcity = txtShipCity.Text;
        o.shippostalcode = txtShipPostalCode.Text;
        o.shipcountry = txtShipCountry.Text;
        o.hide = bool.Parse(rb_Status.SelectedValue);
        od.Order_Update(o);
        LoadData_Orders();
    }

    protected void gvOrders_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {
        Session["OrderID"] = gvOrders.Rows[e.NewSelectedIndex].Cells[0].Text;
        ViewOrders();
    }

    protected void gvOrders_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvOrders.PageIndex = e.NewPageIndex;
        LoadData_Orders();
    }

    protected void ddlFreight_SelectedIndexChanged(object sender, EventArgs e)
    {
        txtFreight.Text = ddlFreight.SelectedValue.ToString().
            Substring(0, ddlFreight.SelectedValue.LastIndexOf('.'));
    }

    #endregion

    #region Order Details
    
    #endregion

    protected void btnInsert2_Click(object sender, EventArgs e)
    {

    }
    protected void btnUpdate2_Click(object sender, EventArgs e)
    {

    }
    
    protected void gvOrders_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {

    }
    protected void gvPayments_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {

    }
    protected void gvPayments_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {

    }
    protected void gvPayments_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {

    }
    protected void dlViewProducts_EditCommand(object source, DataListCommandEventArgs e)
    {

    }
}